// pages/service/withdraw-money/index.js
const handler = require("../../../utils/function.js");
var app = getApp(), api = require('../../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: handler.driverImg,
    money: 0,
    balance: 0,
    maxmoney: 0,
    distribution: '',
    yue: 0
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      yue: options.yue,
      distribution: options.distribution
    })
  },
  onShow: function () {
    var that = this;
    app.request({
      url: api.bank.getdefault,
      method: 'post',
      data: {
        type: 1,
        default: 1
      },
      state: 1,
      success: function (e) {
        console.log(e)
        if (e.code == 1) {
          that.setData({
            data: e.data,
            bank_number: e.data.banknum.slice(-4),
            money: e.data.price,
          })
        }
      }
    })
    app.request({
      url: api.bank.get_maxmoney,
      method: 'post',
      data: {
        type: 1,
        default: 1
      },
      state: 1,
      success: function (e) {
        console.log(e)
        if (e.code == 1) {
          that.setData({
            maxmoney: e.data.price,
          })
        }
      }
    })
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "balance") {
      that.setData({
        balance: inputVlaue
      });
    }
  },
  // 普通提现
  userWithdrawal(money) {
    let that = this
    app.request({
      url: api.user.apply,
      method: 'post',
      data: {
        price: money,
        bcard_num: that.data.data.banknum,
        bankname: that.data.data.bankname,
        type: 1 //提现到银行卡
      },
      success: function (rs) {
        console.log('提现申请结果', rs)
        wx.hideLoading()
        if (rs.code == 1) {
          wx.showToast({
            title: rs.msg,
            icon: 'none',
            mask: true,
            duration: 2000,
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: rs.msg,
            icon: 'none',
            mask: true
          })
        }
      }
    })
  },
  // 分销提现
  distributionWithdrawal(money) {
    let that = this
    app.request({
      url: api.distribution.apply,
      method: 'post',
      data: {
        price: money,
        bcard_num: that.data.data.banknum,
        bankname: that.data.data.bankname,
        type: 1 //提现到银行卡
      },
      success: function (rs) {
        wx.hideLoading()
        if (rs.code == 1) {
          wx.showToast({
            title: rs.msg,
            icon: 'none',
            mask: true,
            duration: 2000,
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: rs.msg,
            icon: 'none',
            mask: true
          })
        }
      }
    })
  },
  sendRequest: function () {
    var that = this;
    var money = that.data.balance;
    var bankInfo = that.data.data;
    console.log("hjsancjd", money, bankInfo);
    wx.showLoading({
      mask: true
    })
    if (that.validate()) {
      // TODO 请求
      //  0提现到余额 1银行卡 2 支付宝 参数nickname：昵称  alipay：支付宝账号
      if (that.data.distribution == 1) {
        that.userWithdrawal(money)
      } else if (that.data.distribution == 2) {
        that.distributionWithdrawal(money)
      }
    }
  },
  validate: function () {
    var that = this;
    var balance = that.data.balance;
    var money = that.data.money;
    if (Number(balance) < 1) {
      wx.showToast({
        title: '取现金额不足1元',
        icon: 'none',
        mask: true
      })
      return false;
    }

    if (Number(balance) > Number(money)) {
      wx.showToast({
        title: '金额大于可提现金额',
        icon: 'none',
        mask: true
      })
      return false;
    }
    return true;
  },
  click: function () {
    console.log('111');
    wx.navigateTo({
      url: "/pages/driver/bankcard-list/index",
    })
  }
})